package com.bigdata.movieticketapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.entity.CinemaType;
import com.bigdata.movieticketapi.entity.MovieType;
import com.bigdata.movieticketapi.mapper.CinemaTypeMapper;
import com.bigdata.movieticketapi.service.ICinemaTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.vo.req.CinemaTypeReqVO;
import com.bigdata.movieticketapi.vo.req.GetDataByIdReqVo;
import com.bigdata.movieticketapi.vo.req.PageCinemaTypeReqVO;
import com.bigdata.movieticketapi.vo.resp.PageCinemaTypeRespVO;
import com.bigdata.movieticketapi.vo.resp.SelectListRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-11-08
 */
@Service
public class CinemaTypeServiceImpl extends ServiceImpl<CinemaTypeMapper, CinemaType> implements ICinemaTypeService {

    @Resource
    private CinemaTypeMapper cinemaTypeMapper;

    @Override
    public void addCinemaType(CinemaTypeReqVO vo) {
        CinemaType cinemaType = query().select("id").last("limit 1").eq("name", vo.getName()).one();
        if (cinemaType != null) {
            throw new BusinessException(ResponseCode.CINEMA_TYPE_ALREADY_EXISTS.getCode(), ResponseCode.CINEMA_TYPE_ALREADY_EXISTS.getMessage());
        }
        CinemaType insertCinemaType = new CinemaType();
        BeanUtils.copyProperties(vo, insertCinemaType);
        insertCinemaType.setSeatTotal(vo.getSeatRow() * vo.getSeatColumn());
        int result = cinemaTypeMapper.insert(insertCinemaType);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void updateCinemaType(CinemaType cinemaType) {
        cinemaType.setSeatTotal(cinemaType.getSeatRow() * cinemaType.getSeatColumn());
        int result = cinemaTypeMapper.updateById(cinemaType);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageCinemaTypeRespVO> pageCinemaTypes(PageCinemaTypeReqVO vo) {
        IPage<PageCinemaTypeRespVO> result = cinemaTypeMapper.selectPageCinemaType(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(), result.getTotal());
    }

    @Override
    public List<SelectListRespVO> getCinemaSelectList() {
        return cinemaTypeMapper.selectCinemaSelectList();
    }


}
